package com.netposa.datawarehouse.aggregator.service.helper;

import com.alibaba.fastjson.JSONObject;
import com.netposa.library.api.IDictionaryCache;
import com.netposa.library.datawarehouse.aggregate.helper.ResourceDefined.ResourceType;
import com.netposa.library.datawarehouse.aggregate.utils.AggregatorUtils;
import com.netposa.library.datawarehouse.aggregate.utils.AggregatorUtils.SearchingColors;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

public class AggregatorServiceHelper {
   public static List<String> perfectCondition(IDictionaryCache dictionaryCache, JSONObject condition) {
      Set<String> modules = new HashSet<>(4);
      String searchContent = condition.getString("searchText");
      if (StringUtils.isNotBlank(searchContent)) {
         if (!searchContent.contains("%") && !searchContent.contains("_")) {
            String plateNumber = AggregatorUtils.plateNumber(searchContent);
            if (StringUtils.isNotBlank(plateNumber)) {
               condition.put("plateNumber", plateNumber);
               modules.add(ResourceType.VEHICLE.getValue());
            }
         } else {
            condition.put("fuzzyPlateNumber", true);
            condition.put("plateNumber", searchContent);
            modules.add(ResourceType.VEHICLE.getValue());
         }
      }

      List words = AggregatorUtils.str2List(searchContent);
      List dictionaries = dictionaryCache.get(SearchingColors.COAT_COLORS.getValue());
      Integer value = AggregatorUtils.convert2ColorCode(dictionaries, words);
      if (!value.equals(0)) {
         condition.put("upperColor", value);
         modules.add(ResourceType.BODY.getValue());
      }

      dictionaries = dictionaryCache.get(SearchingColors.UNDER_CLOTHES_COLORS.getValue());
      value = AggregatorUtils.convert2ColorCode(dictionaries, words);
      if (!value.equals(0)) {
         condition.put("lowerColor", value);
         modules.add(ResourceType.BODY.getValue());
      }

      dictionaries = dictionaryCache.get(SearchingColors.NONMOTOR_COLORS.getValue());
      value = AggregatorUtils.convert2ColorCode(dictionaries, words);
      if (!value.equals(0)) {
         condition.put("nonmotorColor", value);
         modules.add(ResourceType.NONMOTOR.getValue());
      }

      return (List)modules.stream().collect(Collectors.toList());
   }
}
